package com.xinruke.hostels.report.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.po.YwYudingDingdan;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.report.mapper.SingleQueryMapper;
import com.xinruke.hostels.reportinterfaces.dto.SingleConditionQueryDTO;
import com.xinruke.hostels.reportinterfaces.dto.SingleConditionQueryResultDTO;
import com.xinruke.hostels.reportinterfaces.dto.SingleQueryDTO;
import com.xinruke.hostels.reportinterfaces.dto.SingleQueryResultDTO;
import com.xinruke.hostels.reportinterfaces.vo.ConditionVO;
import com.xinruke.hostels.reportinterfaces.vo.EConditionDataType;
import com.xinruke.hostels.reportinterfaces.vo.OrderFieldVO;
import com.xinruke.hostels.reportinterfaces.vo.QueryConditionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SingleQueryService
 * @CreateDate 2019/8/6
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 单一查询Service
 */
@Service
@Slf4j
public class SingleQueryService {
    @Autowired
    SingleQueryMapper singleQueryMapper;

    /**
     * 获取查询和排序的字段信息
     *
     * @param singleConditionQueryDTO
     * @return
     */
    public SingleConditionQueryResultDTO getQueryOrderField(@Valid @RequestBody SingleConditionQueryDTO singleConditionQueryDTO) {
        //TODO 追加当前酒店的条件
        SingleConditionQueryResultDTO singleConditionQueryResultDTO = new SingleConditionQueryResultDTO();
        try {
            List<QueryConditionVO> queryConditionVOList = new ArrayList<>();
            List<OrderFieldVO> orderFieldVOList = new ArrayList<>();

            Integer reportId = singleConditionQueryDTO.getReportId();
            String reportName = "";
            switch (reportId) {
                case 200901:
                    reportName = "预订信息表";
                    queryConditionVOList.add(new QueryConditionVO("dingdanhao", "订单号", EConditionDataType.STRING));
                    orderFieldVOList.add(new OrderFieldVO("id", false));
                    break;
                case 200902:
                    reportName = "入住信息表";
                    queryConditionVOList.add(new QueryConditionVO("id", "编号", EConditionDataType.STRING));
                    orderFieldVOList.add(new OrderFieldVO("dingdanhao", false));
                    break;
            }

            singleConditionQueryResultDTO.setCode(ResultVO.SUCCESS);
            singleConditionQueryResultDTO.setReportId(reportId);
            singleConditionQueryResultDTO.setReportName(reportName);
            singleConditionQueryResultDTO.setOrderFieldVOList(orderFieldVOList);
            singleConditionQueryResultDTO.setQueryConditionVOList(queryConditionVOList);
        } catch (Exception e) {
            singleConditionQueryResultDTO.setCode(ResultVO.FAIL);
            log.error("系统异常：", e);
        }
        return singleConditionQueryResultDTO;
    }

    /**
     * 查询数据列表
     *
     * @param singleQueryDTO
     * @return
     */
    public SingleQueryResultDTO getList(SingleQueryDTO singleQueryDTO) {
        //TODO 追加当前酒店的条件
        SingleQueryResultDTO singleQueryResultDTO = new SingleQueryResultDTO();

        try {
            RowsDataVO rowsDataVO = new RowsDataVO();
            String dataNote = "";
            List<Map<String, Object>> resultList = new ArrayList<>();
            List<String> subtotalList = new ArrayList<>();
            List<Map<String, Object>> totalList = new ArrayList<>();

            //拼装查询条件
            List<ConditionVO> conditionVOList = singleQueryDTO.getConditionVOList();
            Map<String, Object> paramMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(conditionVOList)) {
                for (ConditionVO conditionVO : conditionVOList) {
                    paramMap.put(conditionVO.getField(), conditionVO.getValue());
                }
            }

            //拼装排序
            List<OrderFieldVO> orderFieldVOList = singleQueryDTO.getOrderFieldVOList();
            String orderByStr = "";
            if (!CollectionUtils.isEmpty(orderFieldVOList)) {
                for (OrderFieldVO orderFieldVO : orderFieldVOList) {
                    String field = orderFieldVO.getOrderField();
                    if (StringUtils.isEmpty(field)) {
                        continue;
                    }
                    if (!StringUtils.isEmpty(orderByStr)) {
                        orderByStr = orderByStr + ", ";
                    }
                    if (orderFieldVO.isOrderDesc()) {
                        orderByStr = orderByStr + orderFieldVO.getOrderField() + " desc ";
                    } else {
                        orderByStr = orderByStr + orderFieldVO.getOrderField();
                    }
                }
            }
            if (!StringUtils.isEmpty(orderByStr)) {
                orderByStr = "order by " + orderByStr;
            }

            //根据不同的报表编号，查询数据集
            Page<YwYudingDingdan> page = PageHelper.startPage(singleQueryDTO.getPageNo(), singleQueryDTO.getPageSize() + 1);
            Integer reportId = singleQueryDTO.getReportId();
            switch (reportId) {
                case 200901:
                    dataNote = "订单号：" + paramMap.get("dingdanhao");
                    resultList = singleQueryMapper.getInHotelOrderdList(paramMap, orderByStr);
                    subtotalList.add("shishoujine");
                    subtotalList.add("yajin");
                    totalList = singleQueryMapper.getInHotelOrderdSum(paramMap, orderByStr);
                    break;
                case 200902:
                    resultList = singleQueryMapper.getInHotelOrderdList(paramMap, orderByStr);
                    break;
            }

            rowsDataVO.setPageNo(page.getPageNum());
            rowsDataVO.setTotalRecords(page.getTotal() - 1);
            rowsDataVO.setTotalPages(page.getPages());
            rowsDataVO.setRows(resultList);

            singleQueryResultDTO.setCode(ResultVO.SUCCESS);
            singleQueryResultDTO.setData(rowsDataVO);
            singleQueryResultDTO.setDataNote(dataNote);
            singleQueryResultDTO.setSubtotalList(subtotalList);
            singleQueryResultDTO.setTotalList(totalList);
        } catch (Exception e) {
            singleQueryResultDTO.setCode(ResultVO.FAIL);
            log.error("系统异常，", e);
        }

        return singleQueryResultDTO;
    }

}
